package com.jtp7.demo.controller;


import com.jtp7.demo.aop.annotation.Log;
import com.jtp7.demo.config.SwaggerConfiguration;

import com.jtp7.demo.entity.Permission;
import com.jtp7.demo.entity.User;

import com.jtp7.demo.mapper.UserMapper;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.authority.SimpleGrantedAuthority;

import org.springframework.web.bind.annotation.*;


import javax.servlet.http.HttpServletResponse;
import java.io.FileOutputStream;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@RestController
//@RequestMapping("/user")
@CrossOrigin(allowedHeaders ="*")
//@Secured({"ROLE_manager"}) 权限控制
@Api(value = "用户信息表", tags = {SwaggerConfiguration.TAG_5})
public class UsersController {

//    @Autowired
//    private UserInfoService userInfoService;
//
//    @ApiOperation(value="查询所有用户信息")
//    @GetMapping
//    public ResponseResult<User> selectUserList() {
//        List<User> Userslist = userInfoService.selectUserlist();
//        return new ResponseResult(CommonCode.SUCCESS, Userslist);
//    }

    @Autowired
    UserMapper userMapper;

//    @ApiOperation(value="查找用户信息")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "username", value = "查找用户信息", required = true, paramType = "path", dataType = "String")
//    })
//    @GetMapping(value="/{username}")
//    public ResponseResult<Object> selectUserById(@PathVariable String username) {
//        User users = new User();
//        users.setUsername(username);
//        User users1 = userMapper.selectById(username);
//        return new ResponseResult(CommonCode.SUCCESS,users1) ;
//    }

    @Log
    @PreAuthorize("hasAuthority(T(com.jtp7.demo.config.PermissionContact).USER_VIEW)")
    @ApiOperation(value="查找用户权限")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username", value = "查找用户信息", required = true, paramType = "path", dataType = "String")
    })
    @GetMapping(value="/user/{username}")
    public List<Permission> selectUserById(@PathVariable String username) {
        User users = new User();
        users.setUsername(username);
        List<Permission> permissions = userMapper.selectPermissioin(username);

        if (!permissions.isEmpty()) {
            Set<SimpleGrantedAuthority> sga = new HashSet<>();
            for (Permission permission : permissions) {
                sga.add(new SimpleGrantedAuthority(permission.getPermissionCode()));
            }
            users.setAuthorities(sga);
        }

        return permissions;
    }
    @RequestMapping("list")
    public void list(HttpServletResponse response) throws Exception{
        //需要通过response给前端数据流，设置对应的response
//        response.setContentType("application/vnd.ms-excel");
//        response.setHeader("Content-Disposition","attachment;filename="+"text.xlsx");

        //创建一张工作簿workbook
        Workbook workbook = new XSSFWorkbook();
        //在工作簿中创建一张表sheet
        Sheet sheet = workbook.createSheet("sheet1");
        //在表中创建一行row1，对应是sheet.createRow(0);
        Row row1 = sheet.createRow(0);
        //第一个单元格是权限id，第二个单元格是权限名称，第三个是权限代码
        Cell cell1 = row1.createCell(0);
        cell1.setCellValue("id");
        Cell cell2 = row1.createCell(1);
        cell2.setCellValue("名称");
        Cell cell3 = row1.createCell(2);
        cell3.setCellValue("名称代码");
        //在表中创建第二行row2,对应是sheet.createRow(1);
        int i = 1;
        Row row;
        List<Permission> permissions = userMapper.selectPermissioin("admin");
        for (Permission permission : permissions) {
           row = sheet.createRow(i);
           row.createCell(0).setCellValue(permission.getId());
           row.createCell(1).setCellValue(permission.getPermissionName());
           row.createCell(2).setCellValue(permission.getPermissionCode());
           i++;
        }

        //使用数据流返回给前端
//        OutputStream out = response.getOutputStream();
//        workbook.write(out);
//        out.close();
//        System.out.println("创建Excel完毕");

        //也直接使用文件流
        FileOutputStream fileOutputStream = new FileOutputStream("D:\\tempfile\\test1.xlsx");
        workbook.write(fileOutputStream);
        fileOutputStream.close();
    }

//    @PutMapping
//    @ApiOperation(value="修改用户信息")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "users", value = "修改用户信息", required = true, paramType = "body", dataType = "Users")
//    })
//    public ResponseResult<Object> updateUser(@RequestBody User users) {
//        if (userInfoService.updateUserById(users) != -1) return  new ResponseResult<>(CommonCode.SUCCESS);
//        else return new ResponseResult<>(CommonCode.FAIL);
//    }
//
//    @DeleteMapping(value="/{username}")
//    @ApiOperation(value="删除用户信息")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "users", value = "删除用户信息", required = true, paramType = "body", dataType = "Users")
//    })
//    public ResponseResult<Object> deleteUser(@PathVariable String username) {
//        User users = new User();
//        users.setUsername(username);
//        if (userInfoService.deleteUserById(users) != -1) return  new ResponseResult<>(CommonCode.SUCCESS);
//        else return new ResponseResult<>(CommonCode.FAIL);
//    }
//
//    @PostMapping
//    @ApiOperation(value="新增用户")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "users", value = "新增用户信息", required = true, paramType = "body", dataType = "Users")
//    })
//    public ResponseResult<Object> insertUser(@RequestBody User users) {
//        if ( userInfoService.insertUser(users) != -1) return  new ResponseResult<>(CommonCode.SUCCESS);
//        else return new ResponseResult<>(CommonCode.FAIL);
//    }
//

}
